//
//  CTSegmentView.m
//  ConstellationTalk_0.1
//
//  Created by riley on 15/5/27.
//  Copyright (c) 2015年 riley chu. All rights reserved.
//

#import "CTSegmentView.h"
#import "CTDebugManager.h"
#import "CTHeader.h"
#import "CTDeviceFunction.h"

@interface CTSegmentView ()
{
    UIImageView * segLineImageView;
    
    void (^handlerBlock)(CTSegmentViewType type, UIButton *btn);
}

@end

@implementation CTSegmentView

- (void)dealloc
{
    [segLineImageView removeFromSuperview];
    segLineImageView = nil;
    
    [CTDebugManager showDebugLogInfo:@"dealloc : CTSegmentView Is Removed!!!"];
}

- (instancetype)initWithFrame:(CGRect)frame withVip:(BOOL)isVip withSelf:(BOOL)isSelf
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = CT_HEADER_WHITE_COLOR;
        
        [self creatSubViewsWithVip:isVip withSelf:isSelf];
    }
    return self;
}



-(void)creatSubViewsWithVip:(BOOL)isVip withSelf:(BOOL)isSelf
{
    if (isSelf == YES) {
        //自己
        if (isVip == YES) {

            
            
            UIButton * publishTableBtn = [UIButton buttonWithType:UIButtonTypeCustom];
            publishTableBtn.frame = CGRectMake((CT_HEADER_SCREEN_WIDTH-213)/2.0, 13, 25, 25);
            publishTableBtn.tag = 1;
            [publishTableBtn setBackgroundImage:[UIImage imageNamed:@"liebiao_click"] forState:UIControlStateNormal];
            [publishTableBtn addTarget:self action:@selector(segControlBtnAction:) forControlEvents:UIControlEventTouchUpInside];
            [self addSubview:publishTableBtn];
            
            UIButton * publishCollectionBtn = [UIButton buttonWithType:UIButtonTypeCustom];
            publishCollectionBtn.frame = CGRectMake((CT_HEADER_SCREEN_WIDTH-213)/2.0 +94, 13, 25, 25);
            publishCollectionBtn.tag = 2;
            [publishCollectionBtn setBackgroundImage:[UIImage imageNamed:@"gongge"] forState:UIControlStateNormal];
            [publishCollectionBtn addTarget:self action:@selector(segControlBtnAction:) forControlEvents:UIControlEventTouchUpInside];
            [self addSubview:publishCollectionBtn];
            
            
            UIButton * subjectBtn = [UIButton buttonWithType:UIButtonTypeCustom];
            subjectBtn.frame = CGRectMake((CT_HEADER_SCREEN_WIDTH-213)/2.0 +2*94, 13, 25, 25);
            subjectBtn.tag = 4;
            [subjectBtn setBackgroundImage:[UIImage imageNamed:@"zhuanti"] forState:UIControlStateNormal];
            [subjectBtn addTarget:self action:@selector(segControlBtnAction:) forControlEvents:UIControlEventTouchUpInside];
            [self addSubview:subjectBtn];
            
            
            float seg_width = 0;
            DeviceTypeAdaptation adaptationType = [CTDeviceFunction deviceTypeForResolutionAdaptation];
            switch (adaptationType) {
                case iPhone6:
                case iPhone6_Plus:
                    seg_width = 83;
                    break;
                case iPhone4_4s:
                case iPhone5_5s_5c:
                    seg_width = 60;
                    break;
                default:
                    break;
            }
            
            segLineImageView = [[UIImageView alloc] initWithFrame:CGRectMake(publishTableBtn.frame.origin.x -(seg_width - publishTableBtn.frame.size.width)/2.0, 50, seg_width, 2)];
            segLineImageView.tag = 1000;
            segLineImageView.image = [UIImage imageNamed:@"xuanzhonghengxian"];
            [self addSubview:segLineImageView];
            
            
            
            
            
            
            
            
        }else{
            UIButton * publishTableBtn = [UIButton buttonWithType:UIButtonTypeCustom];
            publishTableBtn.frame = CGRectMake((CT_HEADER_SCREEN_WIDTH-149-25)/2.0, 13, 25, 25);
            publishTableBtn.tag = 1;
            [publishTableBtn setBackgroundImage:[UIImage imageNamed:@"liebiao_click"] forState:UIControlStateNormal];
            [publishTableBtn addTarget:self action:@selector(segControlBtnAction:) forControlEvents:UIControlEventTouchUpInside];
            [self addSubview:publishTableBtn];
            
            UIButton * publishCollectionBtn = [UIButton buttonWithType:UIButtonTypeCustom];
            publishCollectionBtn.frame = CGRectMake((CT_HEADER_SCREEN_WIDTH-149-25)/2.0 + 149, 13, 25, 25);
            publishCollectionBtn.tag = 2;
            [publishCollectionBtn setBackgroundImage:[UIImage imageNamed:@"gongge"] forState:UIControlStateNormal];
            [publishCollectionBtn addTarget:self action:@selector(segControlBtnAction:) forControlEvents:UIControlEventTouchUpInside];
            [self addSubview:publishCollectionBtn];
            
            
            float seg_width = 0;
            DeviceTypeAdaptation adaptationType = [CTDeviceFunction deviceTypeForResolutionAdaptation];
            switch (adaptationType) {
                case iPhone6:
                case iPhone6_Plus:
                    seg_width = 83;
                    break;
                case iPhone4_4s:
                case iPhone5_5s_5c:
                    seg_width = 60;
                    break;
                default:
                    break;
            }
            
            segLineImageView = [[UIImageView alloc] initWithFrame:CGRectMake(publishTableBtn.frame.origin.x -(seg_width - publishTableBtn.frame.size.width)/2.0, 50, seg_width, 2)];
            segLineImageView.tag = 1000;
            segLineImageView.image = [UIImage imageNamed:@"xuanzhonghengxian"];
            [self addSubview:segLineImageView];
        }
    }else{
        //他人
        
        float seg_width = 0;
        DeviceTypeAdaptation adaptationType = [CTDeviceFunction deviceTypeForResolutionAdaptation];
        switch (adaptationType) {
            case iPhone6:
            case iPhone6_Plus:
                seg_width = 83;
                break;
            case iPhone4_4s:
            case iPhone5_5s_5c:
                seg_width = 60;
                break;
            default:
                break;
        }
        
        if (isVip == YES) {
            UIButton * publishTableBtn = [UIButton buttonWithType:UIButtonTypeCustom];
            publishTableBtn.frame = CGRectMake((CT_HEADER_SCREEN_WIDTH-213)/2.0, 13, 25, 25);
            publishTableBtn.tag = 1;
            [publishTableBtn setBackgroundImage:[UIImage imageNamed:@"liebiao_click"] forState:UIControlStateNormal];
            [publishTableBtn addTarget:self action:@selector(segControlBtnAction:) forControlEvents:UIControlEventTouchUpInside];
            [self addSubview:publishTableBtn];
            
            UIButton * publishCollectionBtn = [UIButton buttonWithType:UIButtonTypeCustom];
            publishCollectionBtn.frame = CGRectMake((CT_HEADER_SCREEN_WIDTH-213)/2.0 + 94, 13, 25, 25);
            publishCollectionBtn.tag = 2;
            [publishCollectionBtn setBackgroundImage:[UIImage imageNamed:@"gongge"] forState:UIControlStateNormal];
            [publishCollectionBtn addTarget:self action:@selector(segControlBtnAction:) forControlEvents:UIControlEventTouchUpInside];
            [self addSubview:publishCollectionBtn];
            
            UIButton * subjectBtn = [UIButton buttonWithType:UIButtonTypeCustom];
            subjectBtn.frame = CGRectMake((CT_HEADER_SCREEN_WIDTH-213)/2.0 + 2*94, 13, 25, 25);
            subjectBtn.tag = 4;
            [subjectBtn setBackgroundImage:[UIImage imageNamed:@"zhuanti"] forState:UIControlStateNormal];
            [subjectBtn addTarget:self action:@selector(segControlBtnAction:) forControlEvents:UIControlEventTouchUpInside];
            [self addSubview:subjectBtn];
            
            
            segLineImageView = [[UIImageView alloc] initWithFrame:CGRectMake(publishTableBtn.frame.origin.x -(seg_width - publishTableBtn.frame.size.width)/2.0, 50, seg_width, 2)];
            segLineImageView.tag = 1000;
            segLineImageView.image = [UIImage imageNamed:@"xuanzhonghengxian"];
            [self addSubview:segLineImageView];
        }else{
            
            UIButton * publishTableBtn = [UIButton buttonWithType:UIButtonTypeCustom];
            publishTableBtn.frame = CGRectMake((CT_HEADER_SCREEN_WIDTH-149-25)/2.0, 13, 25, 25);
            publishTableBtn.tag = 1;
            [publishTableBtn setBackgroundImage:[UIImage imageNamed:@"liebiao_click"] forState:UIControlStateNormal];
            [publishTableBtn addTarget:self action:@selector(segControlBtnAction:) forControlEvents:UIControlEventTouchUpInside];
            [self addSubview:publishTableBtn];
            
            UIButton * publishCollectionBtn = [UIButton buttonWithType:UIButtonTypeCustom];
            publishCollectionBtn.frame = CGRectMake((CT_HEADER_SCREEN_WIDTH-149-25)/2.0 + 149, 13, 25, 25);
            publishCollectionBtn.tag = 2;
            [publishCollectionBtn setBackgroundImage:[UIImage imageNamed:@"gongge"] forState:UIControlStateNormal];
            [publishCollectionBtn addTarget:self action:@selector(segControlBtnAction:) forControlEvents:UIControlEventTouchUpInside];
            [self addSubview:publishCollectionBtn];
            
            segLineImageView = [[UIImageView alloc] initWithFrame:CGRectMake(publishTableBtn.frame.origin.x -(seg_width - publishTableBtn.frame.size.width)/2.0, 50, seg_width, 2)];
            segLineImageView.tag = 1000;
            segLineImageView.image = [UIImage imageNamed:@"xuanzhonghengxian"];
            [self addSubview:segLineImageView];
        }
        
        
    }
    
    
}





#pragma mark - action -

-(void)segControlBtnAction:(id)sender
{
    UIButton * senderbtn = (UIButton *)sender;
    
    NSArray * subViewsArray = [self subviews];
    for (UIButton * btn in subViewsArray) {
        switch (btn.tag) {
            case CTSegmentViewTypePublistTable:
                [btn setBackgroundImage:[UIImage imageNamed:@"liebiao"] forState:UIControlStateNormal];
                if (senderbtn.tag == CTSegmentViewTypePublistTable) {
               
                    handlerBlock(CTSegmentViewTypePublistTable,btn);
                    

                    
                }
                break;
            case CTSegmentViewTypePublistCollection:
                [btn setBackgroundImage:[UIImage imageNamed:@"gongge"] forState:UIControlStateNormal];
                if (senderbtn.tag == CTSegmentViewTypePublistCollection) {
       
                    handlerBlock(CTSegmentViewTypePublistCollection,btn);
            
                }
                break;
            case CTSegmentViewTypeFavorite:
                [btn setBackgroundImage:[UIImage imageNamed:@"biaoqing"] forState:UIControlStateNormal];
                if (senderbtn.tag == CTSegmentViewTypeFavorite) {
                //    [senderbtn setBackgroundImage:[UIImage imageNamed:@"biaoqing_click"] forState:UIControlStateNormal];
                    handlerBlock(CTSegmentViewTypeFavorite,btn);
                    
//                    [UIView animateWithDuration:0.3 animations:^{
//                        CGRect rect = segLineImageView.frame;
//                        rect.origin.x = btn.frame.origin.x -(segLineImageView.frame.size.width - btn.frame.size.width)/2.0;
//                        segLineImageView.frame = rect;
//                    }];
                }
                break;
            case CTSegmentViewTypeSubject:
                [btn setBackgroundImage:[UIImage imageNamed:@"zhuanti"] forState:UIControlStateNormal];
                if (senderbtn.tag == CTSegmentViewTypeSubject) {
                   
                    handlerBlock(CTSegmentViewTypeSubject,btn);
                    
                }
                break;
            default:
                break;
        }
        
    }
}


-(void)setPressedHandler:(void (^)(CTSegmentViewType, UIButton *))typeHandeler
{
    handlerBlock = typeHandeler;
}



@end



